package com.itheima.Controller;

import com.itheima.common.entity.Result;
import com.itheima.dto.LoginDTO;
import com.itheima.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 登录
 */
@RestController
@RequestMapping("")
@Slf4j
public class LoginController {
    @Autowired
    private LoginService loginService;

    /**
     * 发送验证码
     *
     * @param telephone
     * @return
     */
    @GetMapping("/validatecode/send4Login")
    public Result send4Login(String telephone) {
        log.info("发送验证码,手机号：{}", telephone);
        try {
            loginService.sendValidateCode4Login(telephone);
            return new Result(true, "验证码发送成功");
        } catch (Exception e) {
            return new Result(false, "验证码发送失败");
        }
    }

    /**
     * 登录
     *
     * @param loginDTO
     * @return
     */
    @PostMapping("member/login")
    public Result login(@RequestBody LoginDTO loginDTO) {
        log.info("验证码登录，手机号：{}，验证码：{}", loginDTO.getTelephone(), loginDTO.getValidateCode());
        try {
            return loginService.login(loginDTO);
        } catch (Exception e) {
            return new Result(false, "登录失败");
        }
    }
}
